
# 维护管理员相关的接口

from api.base import Base
from loguru import logger


class UserManagerApi(Base):


    # 构造方法
    def __init__(self):
        # 初始化url
        self.add_user_url = self.get_url('/admin/admin/create')
        self.edit_user_url = self.get_url('/admin/admin/update')
        self.delete_user_url = self.get_url('/admin/admin/delete')
        self.search_user_url = self.get_url('/admin/admin/list?page=1&limit=20&sort=add_time&order=desc')
        # 临时调用登录，用作调试
        # self.login()

    def add_user(self,username,password,**kwargs):
        """
        新增管理员
        :return:
        """
        # 定义一个请求体 ：
        user_data = {"username":username,"password":password}
        if kwargs:
            logger.info("添加管理员的可选参数:{}".format(kwargs))
            user_data.update(kwargs)            # 将kwargs字典中的数据更新到user_data
        return self.post(self.add_user_url,user_data)

    def edit_user(self,id,username,password,**kwargs):
        """
        编辑管理员
        :return:
        """
        user_data = {'id':id,'username':username,'password':password}
        if kwargs:
            user_data.update(kwargs)
        return self.post(self.edit_user_url,user_data)

    def delete_user(self,id,username,**kwargs):
        """
        删除管理员
        :return:
        """
        user_data = {'id':id,'username':username}
        if kwargs:
            user_data.update(kwargs)
        return self.post(self.delete_user_url,user_data)

    def search_user(self):
        """
        查询管理员
        :return:
        """
        return self.get(self.search_user_url)


if __name__ == '__main__':
    user = UserManagerApi()
    print(user.add_user('test01','123456'))